package com.ruoyi.system.service.honor.impl;

import java.util.List;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.honor.studentpaper.StudentPaper;
import com.ruoyi.system.domain.honor.studentpaper.StudentPaperMember;
import com.ruoyi.system.mapper.honor.StudentPaperMapper;
import com.ruoyi.system.mapper.honor.StudentPaperMemberMapper;
import com.ruoyi.system.service.honor.IStudentPaperService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import static com.ruoyi.common.utils.SecurityUtils.getUserId;

/**
 * 学生科研成果学术论文Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-10-11
 */
@Service
public class StudentPaperServiceImpl implements IStudentPaperService
{
    @Autowired
    private StudentPaperMapper studentPaperMapper;
   @Autowired
   private StudentPaperMemberMapper studentPaperMemberMapper;
    /**
     * 查询学生科研成果学术论文
     * 
     * @param id 学生科研成果学术论文主键
     * @return 学生科研成果学术论文
     */
    @Override
    public StudentPaper selectStudentPaperById(Long id)
    {
        StudentPaper studentPaper = studentPaperMapper.selectStudentPaperById(id);
        StudentPaperMember studentPaperMember = new StudentPaperMember();
        studentPaperMember.setPaperId(id);
        List<StudentPaperMember> studentPaperMembers = studentPaperMemberMapper.selectStudentPaperMemberList(studentPaperMember);
        studentPaper.setMembers(studentPaperMembers);
        return studentPaper;
    }

    /**
     * 查询学生科研成果学术论文列表
     * 
     * @param studentPaper 学生科研成果学术论文
     * @return 学生科研成果学术论文
     */
    @Override
    public List<StudentPaper> selectStudentPaperList(StudentPaper studentPaper)
    {
        //如果是老师，则只显示自己上传的
        if(studentPaper.getCurrentRole()!=null&&studentPaper.getCurrentRole().equals("teacher")){
            studentPaper.setTeacherId(getUserId());
        }
        //如果是部门，则显示老师是系里的所有
        if(studentPaper.getCurrentRole()!=null&&studentPaper.getCurrentRole().equals("department")){
            studentPaper.setDeptId(SecurityUtils.getDeptId());
        }
        List<StudentPaper> studentPapers = studentPaperMapper.selectStudentPaperList(studentPaper);
        for(StudentPaper ra:studentPapers){
            StudentPaperMember studentPaperMember = new StudentPaperMember();
            studentPaperMember.setPaperId(ra.getId());
            List<StudentPaperMember> studentPaperMembers = studentPaperMemberMapper.selectStudentPaperMemberList(studentPaperMember);
            ra.setMembers(studentPaperMembers);
        }
        return studentPapers;
    }

    /**
     * 新增学生科研成果学术论文
     * 
     * @param studentPaper 学生科研成果学术论文
     * @return 结果
     */
    @Override
    public int insertStudentPaper(StudentPaper studentPaper)
    {
        if(studentPaper.getId()!=null){
            StudentPaperMember studentPaperMember = new StudentPaperMember();
            studentPaperMember.setPaperId(studentPaper.getId());
            studentPaperMember.setRank(studentPaper.getRank());
            studentPaperMember.setStudentName(studentPaper.getStudentName());
            studentPaperMember.setStudentType(studentPaper.getStudentType());
            studentPaperMember.setUserType(studentPaper.getUserType());
            studentPaperMemberMapper.insertStudentPaperMember(studentPaperMember);
        }else{
            int result = studentPaperMapper.insertStudentPaper(studentPaper);
            if(result>0){
                StudentPaperMember studentPaperMember = new StudentPaperMember();
                studentPaperMember.setPaperId(studentPaper.getId());
                studentPaperMember.setRank(studentPaper.getRank());
                studentPaperMember.setStudentName(studentPaper.getStudentName());
                studentPaperMember.setStudentType(studentPaper.getStudentType());
                studentPaperMember.setUserType(studentPaper.getUserType());
                studentPaperMemberMapper.insertStudentPaperMember(studentPaperMember);
            }
        }
        return 1;
    }

    /**
     * 修改学生科研成果学术论文
     * 
     * @param studentPaper 学生科研成果学术论文
     * @return 结果
     */
    @Override
    public int updateStudentPaper(StudentPaper studentPaper)
    {
        return studentPaperMapper.updateStudentPaper(studentPaper);
    }

    /**
     * 批量删除学生科研成果学术论文
     * 
     * @param ids 需要删除的学生科研成果学术论文主键
     * @return 结果
     */
    @Override
    public int deleteStudentPaperByIds(Long[] ids)
    {
        for(Long id:ids){
            StudentPaperMember studentPaperMember = new StudentPaperMember();
            studentPaperMember.setPaperId(id);
            List<StudentPaperMember> studentPaperMembers = studentPaperMemberMapper.selectStudentPaperMemberList(studentPaperMember);
            studentPaperMemberMapper.deleteStudentPaperMemberByIds(studentPaperMembers.stream().map(StudentPaperMember::getId).toArray(Long[]::new));
        }
        return studentPaperMapper.deleteStudentPaperByIds(ids);
    }

    /**
     * 删除学生科研成果学术论文信息
     * 
     * @param id 学生科研成果学术论文主键
     * @return 结果
     */
    @Override
    public int deleteStudentPaperById(Long id)
    {
        return studentPaperMapper.deleteStudentPaperById(id);
    }
    /**
     * 根据论文标题查询论文信息
     * @param data
     * @return
     */
    @Override
    public StudentPaper getPaperInfoByTitle(String data) {
        return studentPaperMapper.getPaperInfoByTitle(data);
    }
}
